<template>
  <el-dialog
    :title="!deptForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="deptForm" :rules="dataRule" ref="deptForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="上级部门" prop="parentId">
        <el-popover
          ref="deptListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="departmentDataTree"
            :props="deptListTreeProps"
            node-key="id"
            ref="deptListTree"
            @current-change="deptListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="deptForm.parentName" v-popover:deptListPopover :readonly="true" placeholder="点击选择上级部门"
                  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="办公电话" prop="phone">
        <el-input v-model="deptForm.phone" placeholder="办公电话"></el-input>
      </el-form-item>
      <el-form-item label="Location" prop="address">
        <el-input v-model="deptForm.address" placeholder="办公室地点"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      deptForm: {
        id: 0,
        parentId: 0,
        parentName: '',
        name: '',
        phone: '',
        address: '',
      },
      dataFromDepartment: [],
      // 树形结构
      departmentDataTree: [],
      deptListTreeProps: {
        label: 'name',
        children: 'children'
      },
      dataRule: {
        name: [
          {required: true, message: '部门名称不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init (id) {
      // 有id为修改，没有id则为添加
      this.deptForm.id = id || 0
      this.departmentDataTree = this.dataFromDepartment
      this.visible = true
      this.$nextTick(() => {
        this.$refs['deptForm'].resetFields()
        // 添加
        if (!this.deptForm.id) {
          this.deptListTreeSetCurrentNode()
          this.deptForm.parentName = ''
          this.deptForm.parentId = 0
        } else {
          this.$http({
            url: this.$http.adornUrl(`/sys/department/info/${this.deptForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.deptForm.parentId = data.department.parentId
              this.deptForm.name = data.department.name
              this.deptForm.address = data.department.address
              this.deptForm.phone = data.department.phone
              this.deptListTreeSetCurrentNode()
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['deptForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/department/${!this.deptForm.id ? 'add' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.deptForm.id || undefined,
              'name': this.deptForm.name,
              'address': this.deptForm.address,
              'phone': this.deptForm.phone,
              'parentId': this.deptForm.parentId
            })
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 部门树选中节点
    deptListTreeCurrentChangeHandle (data, node) {
      this.deptForm.parentId = data.id
      this.deptForm.parentName = data.name
    },
    // 部门树显示当前选中节点
    deptListTreeSetCurrentNode () {
      this.$refs.deptListTree.setCurrentKey(this.deptForm.parentId)
      this.deptForm.parentName = (this.$refs.deptListTree.getCurrentNode() || {})['name']
    },
  }
}
</script>

<style scoped>

</style>
